<?php

class AnalyzeArray extends AppLog
{
	var $error = '';
	var $cols = array();
	var $colsOrg = array();
	var $redirCols = array();
	var $typCols = array();
	var $typRows = array();
	var $rows = array();
	var $values;
	var $cntCol;
	var $fieldSep = '/\|/';
	
	function initCols(&$tempCols)
	{
		$this->cntCol = 0;
		$this->cols = array();
		$this->colsOrg = array();
		$this->redirCols = array();
		$this->typCols = array();
		$this->typRows = array();
		$colsSep = preg_split( $this->fieldSep, $tempCols);
		
		foreach( $colsSep as &$col )
		{
			//$this->dbgWrite($this->cntCol);
			$this->cols[ strtoupper($col) ] = $this->cntCol;
			$this->colsOrg[ strtoupper($col) ] = $col;
			$this->typCols[ strtoupper($col) ] = '';
			$this->cntCol++;
		}
	}
	
	function redirectCols( $src, $dst)
	{
		$this->redirCols[ strtoupper($src) ] = $dst;
	}

	function typeCols( $src, $typ)
	{
		$this->typCols[ strtoupper($src) ] = $typ;
	}

	function typeRows( $col, $dst )
	{
		$this->typRows[ strtoupper($dst) ] = $this->cols[ strtoupper($col) ] ;
	}

	function valuesRow($row)
	{
		$this->values = preg_split( $this->fieldSep, $row);
	}

	function valueRowsAssoc( &$ary, &$ary1 )
	{
		foreach( $ary as $key => $val )
		{
			if ( $val != '' )
			{
				$tmp = preg_split("/\|/", $val);
				$ary1[ $tmp[0] ] = $tmp[1];
			}
		}
	}
	
	function valueAssoc( $fld, $value, &$ary )
	{
		$tmp = preg_split("/\|/", $fld);
		foreach( $ary[ $tmp[0] ] as $key => $val )
		{
			if ( $val == $value ) return $ary[ $tmp[1] ][$key];
		}
	}

	function pushAllCol(&$aryDst ,$nRow = -1)
	{
		foreach( $this->cols as $key => $val )
		{
			if ( isset($this->redirCols[ $key ]) )
			{
				$aryDst[ $this->redirCols[ $key ] ] = $this->pushCol( $key, $nRow, $this->typCols[ $key ] );
			}
			else
			{
				$aryDst[ $this->colsOrg[ $key ] ] = $this->pushCol( $key, $nRow, $this->typCols[ $key ] );
			}
		}
	}

	function pushSelCol(&$aryDst, $selCols = "" ,$nRow = -1, $type = "")
	{
		$colsSep = preg_split( $this->fieldSep, $selCols);
		foreach( $colsSep as $val )
		{
			if ( isset($this->redirCols[ strtoupper($val) ]) )
			{
				//print $this->redirCols[ strtoupper($val) ]." - ".strtoupper($val)."<br>";
				$aryDst[ $this->redirCols[ strtoupper($val) ] ] = $this->pushCol( $val, $nRow, $type );
				//print_r($aryDst[ $this->redirCols[ strtoupper($val) ] ]);
			}
			else
			{
				$aryDst[ $this->colsOrg[ strtoupper($val) ] ] = $this->pushCol( $val, $nRow, $type );
			}
		}
	}

	function pushCol($col, $nRow = -1, $type = '')
	{
		$exit = count( $this->rows );
		$cRow = 0;
		$row = "";
		$tmpValue = array();
		while( $exit-- )
		{
			$row = $this->rows[ $cRow ];
			if ( preg_match( "/^END_ROWS/", $row ) )
			{
				break;
			}
			else
			{
				if ( $nRow == -1 || $nRow == $cRow )
				{
					$tmpValueCols = preg_split( $this->fieldSep, $row);
					//$this->logWrite(join(';',$tmpValueCols));
					if ( isset( $this->typRows[ strtoupper($col) ] ) ) $type = $tmpValueCols[ $this->typRows[ strtoupper($col) ] ];
					switch( $type )
					{
						case 'checked':
							//$this->logWrite('>'.$tmpValueCols[ $this->cols[ strtoupper($col) ] ].'<');
							$tmpValue[] = ($tmpValueCols[ $this->cols[ strtoupper($col) ] ] == 'S' ) ? 'checked' : '' ;
							break;
							
						case 'text':
							$tmpValue[] = preg_replace("/\\0/", "\n", $tmpValueCols[ $this->cols[ strtoupper($col) ] ]);
							break;
							
						case 'timestamp':
							$tmpValue[] = date('d/m/Y H.i.s' , $tmpValueCols[ $this->cols[ strtoupper($col) ] ] );
							break;
							
						default:
							$tmpValue[] = $tmpValueCols[ $this->cols[ strtoupper($col) ] ];
					}
				}
				if ( $nRow == $cRow ) break;
			}
			$cRow++;
		}

		if ( $nRow == -1 )
			return $tmpValue;
		else
			return $tmpValue[0];
	}

/* 
	cancella le righe fino alla segnalazione inserita in $end
	$end = numeric fino as un numero di riga
	$end = string fino ad incontrare una determinata stringa
 */
	function deleteRows( $end )
	{
		$cRow = 0;
		$tmpValue = array();
		$row = " ";
		reset( $this->rows );
		//$this->dbgWrite("deleteRows ".count($this->rows));
		while( count( $this->rows ) )
		{
			$row = array_shift( $this->rows );
			if ( is_numeric( $end) )
			{
				$cRows++;
				if ( $cRows == $end ) break;
			}
			elseif ( $row == $end )
			{
				//$this->dbgWrite("deleteRows ".count($this->rows));
				break;
			}
		}
	}

	function pushField($col)
	{
		$tmpValue = array();
		foreach( $this->rows as &$row)
		{
			if ( $row != "" )
			{
				$tmpValueCols = preg_split( $this->fieldSep, $row);
				$tmpValue = $tmpValueCols[ $this->cols[ strtoupper($col) ] ];
			}
		}
		return $tmpValue;
	}

	function valueCol($col)
	{
		return $this->values[ $this->cols[ strtoupper($col) ] ];
	}

	function chomp(&$string) 
	{ 
		$endchar = substr("$string", strlen("$string") - 1, 1); 
		if ($endchar == "\n") 
		{ 
				$string = substr("$string", 0, -1); 
		} 
	} 
}

?> 
